<?php require_once 'header.php'; ?>
<div class="container">
<div id="content" class="col-lg-9 clearfix">
			<?php if(isset($error)):?>
				<p class="error"><?=$error?></p>
			<?php endif;?>	
			<?php if(isset($success)):?>
				<p class="success"><?=$success?></p>
			<?php endif;?>
			<div class="grid_4">
		        <p>
		            <label>Name</label>
		            <input type="text" name="name" id="name" value="<?php if (isset($name) && $name) echo $name; ?>" />
		        </p>
		    </div> 
		    <div class="grid_4">
		        <p>
		            <label>Category</label>
		            <select name="category">
		                <option value="0">All</option>
		                <?php foreach ($categorys as $category): ?>
                            <option value="<?= $category->category_id ?>" <?php if ($category_filter == $category->category_id) echo "selected='selected'"; ?>>
                            <?= $category->category_name ?>
                            </option>
                    	<?php endforeach; ?>
		            </select>    
		        </p>
		    </div>
		    <div class="grid_4">
		        <p>
		            <label>Sub Category</label>
		            <select name="sub_category">
		                <option value="0">All</option>
		                <?php foreach ($sub_categorys as $sub_category): ?>
                            <option value="<?= $sub_category->sub_category_id ?>" <?php if ($sub_category_filter == $sub_category->sub_category_id) echo "selected='selected'"; ?>>
                            <?php echo $sub_category->sub_category_name ?>
                            </option>
                    	<?php endforeach; ?>
		            </select>    
		        </p>
		    </div>
		   <div class="grid_4">
		        <p>
		            <label>Publish</label>
		            <select name="status">
		                <option value="-1" selected='selected'>All</option>
		                <option value="1" <?php if($status==1) echo "selected='selected'"?>>Bật</option>
		                <option value="0" <?php if($status==0) echo "selected='selected'"?>>Tắt</option>
		            </select>
		        </p>
		    </div>
		    <div class="grid_4">
		        <p>
		            <label>Limit</label>
		            <select name="limit">
		                <option value="10" <?php if ($limit == 10) echo 'selected="selected"'; ?>>10</option>
		                <option value="25" <?php if ($limit == 25) echo 'selected="selected"'; ?>>25</option>
		                <option value="50" <?php if ($limit == 50) echo 'selected="selected"'; ?>>50</option>
		                <option value="100" <?php if ($limit == 100) echo 'selected="selected"'; ?>>100</option>
		                <option value="500" <?php if ($limit == 500) echo 'selected="selected"'; ?>>500</option>
		            </select>
		        </p>
		    </div>    
		    <div class="grid_2">
		        <p>
		            <label>Lọc</label>
		            <input type="button" onclick="filter();" value="Filter" />
		        </p>
		    </div>
		    <div style="clear:both"></div>
			<div class="grid_10">
				<h2>Danh sách topics</h2>
			</div>
			<div class="grid_6" style="text-align: right;">
				<h2>
					<a href="<?php echo site_url('topic/add')?>"><img src="<?=base_url()?>styles/admin/Add.png" height="27px" /></a>
					<a href="javascript:deletetopic();"><img src="<?=base_url()?>styles/admin/Delete.png" height="27px" /></a>
					<a href="javascript:publishtopic();"><img src="<?=base_url()?>styles/admin/Open.gif" height="27px" /></a>
					<a href="javascript:unpublishtopic();"><img src="<?=base_url()?>styles/admin/Closed.png" height="27px" /></a>
				</h2>
			</div>
			<script type="text/javascript">
			function deletetopic() {
				var confirm = window.confirm('Bạn có chắc chắn xóa topic (s) này không???');
				if(!confirm) return ;
				$('#list_topic').attr('action', '<?php echo site_url('topic/delete')?>');
				$('#list_topic').submit();
			}
			function publishtopic() {
				//var confirm = window.confirm('Bạn có chắc chắn bật ứng dụng (s) này không???');
				//if(!confirm) return false;
				$('#list_topic').attr('action', '<?php echo site_url('topic/publish')?>');
				$('#list_topic').submit();
			}	
			function unpublishtopic() {
				//var confirm = window.confirm('Bạn có chắc chắn tắt ứng dụng (s) này không???');
				//if(!confirm) return false;
				$('#list_topic').attr('action', '<?php echo site_url('topic/unpublish')?>');
				$('#list_topic').submit();
			}		
			// publish process
			function publishID(topic_id, value) {
				$.ajax({
					type: "POST",
					data: "id=" + topic_id + "&value=" + value,
					url: "<?php echo site_url('topic/publishID')?>",
					beforeSend: function() {
						$("#publish" + topic_id).html("working");
					},
					success: function(response) {
						$("#publish" + topic_id).html(response);
					} 
				});
			}
			function removetopic(id) {
	        var conf = window.confirm('Delete this topic?');
	        if(!conf)
	            return;
	        $.ajax({
	            type: "POST",
	            data: "id=" + id,
	            url: "<?= site_url('topic/deletetopic') ?>",
	            beforeSend: function() {
	                $("#a" + id).html("working");
	            },
	            success: function(response) {
	                var html = $.parseJSON(response);
	                alert(html.msg);
	                $("#tr" + id).hide();
	            }
	        });
    }
			</script>
			
			<form id="list_topic" method="post">
			<div class="grid_16">
				<table>
					<thead>
						<tr id = "view-pagination">
		                    <td colspan="6" align = "right" class="headt">Total topic <?=$totaltopic?></td> 
		                    <td colspan="6" align = "left" class="headt"><?= $this->pagination->create_links() ?></td>
		                </tr>
						<tr>
							<th width="1">
								<input style="width: 5px;" type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);" />
							</th>
							<th>Id</th>
							<th>Title</th>
                            <th>Category</th>
							<th>Sub category</th>
							<th>Link</th>
							<th>Type</th>
							<th>Number</th>
							<th>Description</th>
							<th width="30%">Content</th>
							<th>Create date</th>
							<th>Picture</th>
							<th>Video</th>
							<th>Author</th>
							<th>Status</th>
							<th>Total view</th>
							<th>Top</th>
							<th>Tag</th>
							<th>Actions</th>
						</tr>
					</thead>
					<tbody>
						<?php if($topics):?>
						<?php foreach($topics as $topic):?>
						<tr id='tr<?=$topic->topic_news_id?>'>
							<td><input style="width: 20px;" type="checkbox" name="selected[]" value="<?=$topic->topic_news_id;?>" />
							<td>
								<input type="hidden" name="id[]" value="<?=$topic->topic_news_id?>" />
								<?=$topic->topic_news_id?>
							</td>
                            <td><?=$topic->topic_news_title?></td>
                            							<td>
                                <?php
                                $CI = & get_instance();
                                $CI->load->model('category_model', 'category');
                                $category = $CI->category->getInfo($topic->category_id);
                                if ($category)
                                    echo $category->category_name;
                                ?>
                            </td>
                            <td>
                                <?php
                                $sub_category = $CI->category->getSubInfo($topic->sub_category_id);
                                if ($sub_category)
                                    echo $sub_category->sub_category_name;
                                ?>
                            </td>
                            <td><?=$topic->topic_news_links?></td>
                            <td><?=$topic->topic_news_type?></td>
                            <td><?=$topic->topic_news_number?></td>
                            <td><?=$topic->topic_news_desc?></td>
                            <td><?=substr(strip_tags($topic->topic_news_content), 0,250).'...'?></td>
                            <td><?=date('Y-m-d H:i:s', $topic->topic_news_date)?></td>
							<td valign="middle">
								<img src='<?php echo base_url() . $topic->topic_news_picture?>' width='60px' height='60px' style='vertical-align: middle;' />
							</td>
							<td><?=$topic->topic_news_video?></td>
                            <td><?=$topic->topic_news_author?></td>
                
							<td id="publish<?=$topic->topic_news_id?>">
								<?php 
										if($topic->topic_news_status == 1) {
											echo '<a href="javascript:;" onclick="publishID(' . $topic->topic_news_id . ',0);">Bật</a>';
										} else {
											echo '<a href="javascript:;" onclick="publishID(' . $topic->topic_news_id. ',1);">Tắt</a>';
										}
									?>
							</td>
							<td><?=$topic->total_view?></td>
                            <td><?=$topic->top?></td>
                            <td><?=$topic->topic_news_tag?></td>
							<td>
								<a href="<?php echo site_url('topic/edit/' . $topic->topic_news_id)?>" class="edit">Edit</a>
								<a href="javascript:;" onclick="removetopic('<?= $topic->topic_news_id ?>');" class="delete" id="a<?= $topic->topic_news_id ?>">Delete</a>
							</td>
						</tr>
						<?php endforeach;?>
						<?php endif;?>
					</tbody>
				</table>
						<?php if(!$topics):?>
							<center>Không tìm thấy dữ liệu</center>
						<?php endif;?>
				</div>
				</form>
</div>
    </div>
<script type="text/javascript">
    function filter() {
        var url = '<?php echo site_url('topic/viewall') ?>' + '/<?php echo "$sort/$order/"; ?>' ;

        var name = $('input[name=name]').val();
        name = (name=='')?0:name;
        url += encodeURIComponent(name) + '/';
        
        var category = $('select[name=category]').val();
        category = (category=='')?0:category;
        url += encodeURIComponent(category) + '/';

        var sub_category = $('select[name=sub_category]').val();
        sub_category = (sub_category=='')?0:sub_category;
        url += encodeURIComponent(sub_category) + '/';
        
        var status = $('select[name=status]').val();
        url += encodeURIComponent(status) + '/';
        
        var limit = $('select[name=limit]').val();
        url += encodeURIComponent(limit) + '/';
        //alert(url);
        window.location.href = url;
    }  
</script>
<?php require_once 'footer.php'; ?>